/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* _reConstructBinaryTree(vector<int>& pre, int& rooti, vector<int>& vin, int front, int back)
    {
        if (front > back || rooti >= pre.size())
            return nullptr;
        TreeNode* root;
        root = new TreeNode(pre[rooti]);
        int rootsig = front;
        while (rootsig <= back)
        {
            if (pre[rooti] == vin[rootsig])
                break;
            else
                rootsig++;
        }
        rooti++;
        root->left = _reConstructBinaryTree(pre, rooti, vin, front, rootsig - 1);
        root->right = _reConstructBinaryTree(pre, rooti, vin, rootsig + 1, back);
        return root;
    }
    TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
        TreeNode* root;
        int front = 0;
        int back = vin.size() - 1;
        int rooti = 0;
        root = _reConstructBinaryTree(pre, rooti, vin, front, back);
        return root;
    }
};